var pool = require("./config/db.js")

/**
 * 国家
 * @param countries
 * @constructor
 */
function Countries(countries) {
    if(countries){
        this.id = countries.id;
        this.country = countries.country;
        this.flag= countries.flag;
        this.country_code = countries.country_code;
    }
};

module.exports = Countries;

/**
 * 增
 * @param callback
 */
Countries.prototype.save = function(callback) {
    var _countries = {
        country: this.country,
        flag: this.flag,
        country_code: this.country_code
    };
    pool.getConnection(function(err,connection){
        connection.query('INSERT INTO `countries` SET ?',
        _countries,
        function (err, rows, fields) {
            if (err) throw err;
            connection.release();
            callback(null,rows);
        });
    });

};

/**
 * 删一个
 * @param callback
 */
Countries.prototype.delete = function(id,callback) {
    var _id= id;
    pool.getConnection(function(err,connection){
        connection.query('DELETE FROM `countries` WHERE id=?',
        [_id],
        function (err, rows, fields) {
            if (err) throw err;
            connection.release();
            callback(null,rows);
        });
    });
};

/**
 * 改一个
 * @param callback
 */
Countries.prototype.update = function(callback) {
    var _countries = {
        id:this.id,
        country: this.country,
        flag: this.flag,
        country_code: this.country_code
    };

    pool.getConnection(function(err,connection){
        connection.query('UPDATE `countries` SET ? WHERE id =?',
            [
                _countries,
                _countries.id
            ],
        function (err, rows, fields) {
            if (err) throw err;
            connection.release();
            callback(null,rows);
        });
        //console.log(_query.sql);
    });
};

/**
 * 查一个
 * @param callback
 */
Countries.prototype.getOne = function(id,callback) {
    var _id= id;
    pool.getConnection(function(err,connection){
        connection.query('SELECT * from `countries` WHERE id=?',
            [_id]
            , function (err, rows, fields) {
                if (err) throw err;
                connection.release();
                callback(null,rows);
        });
    });
};

/**
 * 分页查
 * @param pagesize 每页个数
 * @param pages    当前页
 * @param callback
 */
Countries.getTen = function(pagesize,pages,callback) {
    var _size = pagesize ? pagesize  : 10,
    _page = pages ? pages : 1,
        _total = null,
        _totalPage=null;

    pool.getConnection(function(err,connection){
        if(err){
            callback("连接池出错");
        }
        if(_total == null){
                connection.query('SELECT COUNT(*) FROM `countries`',function(err,rows,fields){
                    _total = rows[0]['COUNT(*)'];
                    _totalPage = _total / _size;
                    _query();
                });
            }
        var _query = function(){
            connection.query('SELECT * from `countries` ORDER BY `id` DESC LIMIT ?,?',
                [_totalPage>=1 ? (_page-1)*_size : 0,
                    _totalPage <_page ? (_page*_size) :_size]
                , function (err, rows, fields) {
                    if (err){
                        callback("查询出错");
                    }
                    connection.release();
                    callback(null,rows,_totalPage);
                });
        }
    });
};
